/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package model.mail.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.ResourceBundle;
import javax.mail.internet.MimeMessage;
import javax.servlet.ServletContext;
import model.bean.Bean;
import model.mail.IMailSenderUtil;
import org.apache.log4j.Logger;
import org.apache.velocity.app.VelocityEngine;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.mail.javamail.MimeMessagePreparator;
import org.springframework.ui.velocity.VelocityEngineUtils;
import org.springframework.web.context.support.ServletContextResource;

/**
 * @Archivo MailSenderUtil.java
 * @Fecha                @Autor   @Cambios
 * 21/01/2011 02:23:30 PM       moshiro
 */
public class MailSenderUtil implements IMailSenderUtil {

    Logger logger = Logger.getLogger(this.getClass());
    private VelocityEngine velocityEngine;
    private JavaMailSenderImpl mailSender;
    private String from;

    public void setVelocityEngine(VelocityEngine velocityEngine) {
        this.velocityEngine = velocityEngine;
    }

    public void setMailSender(JavaMailSenderImpl mailSender) {
        this.mailSender = mailSender;
    }

    public void setFrom(String from) {
        this.from = from;
    }
    
    public void sendMail(ServletContext servletContext, final Bean vo, final String template, final String subject, final String objectName, final String to, String... images) {
        Map<String, Object> model = new HashMap<String, Object>();
        model.put(objectName, vo);
        sendMail(servletContext, model, template, subject, to, images);
    }

    public void sendMail(final ServletContext servletContext, final Map<String, Object> model, final String template, final String subject, final String to, String... images) {
        final List<String> newImages;

//        if (images == null || images.length == 0) {
//            ResourceBundle rb = ResourceBundle.getBundle("images");
//            newImages = new ArrayList<String>();
//            newImages.add(rb.getString("mail.image0"));
//            newImages.add(rb.getString("mail.image1"));
//        } else {
//            newImages = Arrays.asList(images);
//        }
        MimeMessagePreparator preparator = new MimeMessagePreparator() {

            public void prepare(MimeMessage arg0) throws Exception {
                MimeMessageHelper message = new MimeMessageHelper(arg0, true);
                message.setTo(to);
                message.setFrom(from);

                model.put("anio", (new java.text.SimpleDateFormat("yyyy")).format((new java.util.Date())));

                String text = VelocityEngineUtils.mergeTemplateIntoString(
                        velocityEngine, template, model);
                message.setSubject(subject);
                message.setText(text, true);

//                ServletContextResource resource;
//                FileSystemResource res;
//                int i = 0;
//                for (String image : newImages) {
//                    resource = new ServletContextResource(servletContext, image);
//                    res = new FileSystemResource(resource.getFile());
//                    message.addInline("image" + i++, res);
//                }
            }
        };
        this.mailSender.send(preparator);
    }
}
